var plugin = requirePlugin("ykb_measure_plugin")

Page({
  data: {
    realTimeWeight: "",
    bleState: "空闲",
    state: "paused",
    measureData: null,
    score: "0"
  },
  onLoad: function() {
    
    plugin.init({
      height: 170,
      gender: 1,
      birthday: "2019-01-01",
      //person_body_shape: 1,
      //person_goal: 2,
      //timeout: 3500,
      /* appid: "mpplugin",
      secret: "1c98652952a92301",
      tipToast: false, */
      appid: "herbalifemp", //康宝莱appid
      secret: "c2882deddc2d9c95",
      /* version: 2,
      overtime: 10000 */
    },data => {
      let { code, msg, bleState, state, realTimeWeight } = data
      console.log(code, msg)
      if (code >= 1003 && code <= 1009) {
        if (realTimeWeight != undefined) {
          this.setData({
            realTimeWeight
          })
        }        
        if (state) {
          this.setData({
            state
          })
        }
        this.setData({
          bleState
        })
      } else if (code == 1010) {
        this.setData({
          bleState,     
          state,     
          score: data.result.score,
          measureData: data.result
        })
        console.log("impedance", data.impedance)
      }
    })
    plugin.start()
  },
  stop() {
    plugin.stop()
  },
  onHide() {
    //plugin.stop()
  },
  onShow() {
    

    let obj = {
      birthday: "1990-01-01",
      weight: 60,
      bmi: 20.6/*  */,
      height: 170,
      gender: 1,      
      bodyfat: 19.8,
      water: 57.9,
      protein: 18.32,
      bmr: 1568,
      bodyAge: 24,
      bodyShape: 4,
      bone: 2.6,
      muscle: 55.7,
      subfat: 15.8,
      lbm: 41.56,
      visfat: 6,
      heartRate: 61,
      heartIndex: 3.5,
      muscleMass: 53.51
    }
    plugin.reportData(obj).then(data => {      
      this.setData({
        measureData: data
      })
    })
  },
  restart() {
    plugin.restart()
  }
})